package com.keepers.repair.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.ServiceUtils;
import com.keepers.repair.net.LogUtil;
import com.keepers.repair.service.ScreenManager;
import com.keepers.repair.service.ScreenReceiverUtil;
import com.keepers.repair.service.SocketService;

/**
 * 1像素Activity
 */

public class SinglePixelActivity extends AppCompatActivity {

    private ScreenReceiverUtil screenReceiverUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.e("onCreate--->启动1像素保活");
        Window mWindow = getWindow();
        mWindow.setGravity(Gravity.START | Gravity.TOP);
        WindowManager.LayoutParams attrParams = mWindow.getAttributes();
        attrParams.x = 0;
        attrParams.y = 0;
        attrParams.height = 1;
        attrParams.width = 1;
        mWindow.setAttributes(attrParams);
        // 绑定SinglePixelActivity到ScreenManager
        ScreenManager.getScreenManagerInstance().setSingleActivity(this);
        screenReceiverUtil = new ScreenReceiverUtil(this);
        screenReceiverUtil.setScreenReceiverListener(new ScreenReceiverUtil.ScreenStateListener() {
            @Override
            public void onScreenOn() {
                LogUtil.e(" SinglePixelActivity onScreenOn");
                if (!ServiceUtils.isServiceRunning(SocketService.class)) {
                    Intent mIntent = new Intent(SinglePixelActivity.this, SocketService.class);
                    startService(mIntent);
                }
            }

            @Override
            public void onScreenOff() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        LogUtil.e("onDestroy--->1像素保活被终止");
        super.onDestroy();
        screenReceiverUtil.stopScreenReceiverListener();
    }
}
